<?php
class Zen_View_Helper_Jqgrid_Widget extends Zend_View_Helper_Abstract {

	private $columns;
	private $js;
	private $table_id;
	private $base_url;

	public function init(){
	}
	
	public function jqgrid_Widget($base_url,$columns,array $params = array()) {
		$this->base_url = $base_url;
		if(isset($params['rows'])){
            $params['limit']=$params['rows'];
        }
        if(isset($params['sidx'])){
            $params['order']=$params['sidx'];
        }
        if(isset($params['sord'])){
            $params['dir']=$params['sord'];
        }
        if((isset($params['_search']))&&($params['_search']=='true')){
            $params['search']=true;
        }else 
            $params['search']=false;
		$this->init();
		$table_id='jqgrid_'.md5($this->base_url);
		$this->table_id = $table_id;
		$jquery = $this->view->jQuery();
        $jquery->enable(); // enable jQuery Core Library
		Zen_Script::add('/cms/library/Zen/View/Helper/Jqgrid/js/i18n/grid.locale-ru.js');
		Zen_Script::add('/cms/library/Zen/View/Helper/Jqgrid/js/jquery.jqGrid.min.js');
		Zen_Style::add('/cms/library/Zen/View/Helper/Jqgrid/css/ui.jqgrid.css');
        $url = $this->base_url . '/operation/read/';
		$default_options = array (
			'url' => $url, 
			'editurl' => '', 
			'datatype' => 'json', 
			'autowidth' => true, 
			'height' => '100%', 
			'hidegrid' => false, 
			'rowNum' => 20, 
			'rowList' => array (5, 10, 20, 30, 40, 50 ), 
			'caption' => '&nbsp;', 
			'sortname' => 'id', 
			'viewrecords' => true, 
			'sortorder' => 'desc', 
			'pager' => "#pager_" . $table_id 
		);
		$options = array_merge ( $default_options, $params );
		$controls = $this->renderControls();
		if (sizeof ( $columns ) > 0) {
			$table = "<table id='" . $table_id . "'></table> ";
			$pager = "<div id='pager_" . $table_id . "'></div>";
			$options ['colModel'] = $columns;
			$grid = json_encode ( $options );
			$grid = "<script type='text/javascript'>
            $(document).ready(function(){
               	$('#" . $table_id . "').jqGrid(" . $grid . ");
            	$('#" . $table_id . "').jqGrid('navGrid','#pager_" . $table_id . "',
            	{edit:false,add:false,del:false,search:true},
            	{}, // edit options 
            	{}, // add options 
            	{}, // del options 
            	{closeOnEscape:true,jqModal:false}//prmSearch
            	);
            	var table_cont=$('#gbox_" . $table_id . "').parent();
            	var w=table_cont.width();
  				$('#" . $table_id . "').setGridWidth(w);
            	$(window).resize(function(){
            		var table_cont=$('#gbox_" . $table_id . "').parent();
            		var w=table_cont.width();
  					$('#" . $table_id . "').setGridWidth(w);
				});
        	});
            </script>";
			$res = $controls . $table . $pager . $grid;
			$jquery->addOnLoad($this->js);
			return $res;
		}
	}

	protected function renderControls(){
		$table_id = $this->table_id;
		$controls=array();
		$type = 'jq_table';
		$helper = new Zend_View_Helper_Url();
		$baseUrl = $this->base_url;
		$js = '';
		
		$controls = array(
			array(
				'label' =>  'Добавить',
				'uri'	=>  $baseUrl . '?operation=create', 
				'ico'	=>	'icon-plus icon-white', 
				'title'	=>  'Добавление нового элемента',
				'id'	=>  'create-'.$type, 
				'class'	=>	'create-type btn btn-primary',
			),
			array(
				'uri'	=> $baseUrl . '?operation=update', 	
	            'label' => 'Редактировать',
				'ico'	=> 'icon-pencil icon-white',
	            'title'	=> 'Редактирование элемента',  
	            'id'	=> 'update',
	        	'class' => 'btn btn-success',
      		),
      		array(
				'uri'	=> $baseUrl . '?operation=delete', 	
	            'label' => 'Удалить',
	            'title'	=> 'Удалить элемент',  
	            'id'	=> 'delete',
	        	'class' => 'btn btn-danger',	
	            'ico'	=> 'icon-trash icon-white',
	        )
		);
		
		
		
		$js.='
			$("#admin-update").removeClass("disabled").click(function(){
				var id=$("#'.$this->table_id.'").jqGrid("getGridParam","selrow");
				if( id != null ) {
					var href = $(this).attr("href");
					href = href + "&id=" + id;
					$(this).attr("href",href);
				}else{
					alert("Выберите строку для редактирования");
					return false;
				}
			});
			$("#admin-delete").removeClass("disabled").click(function(){
				if(!$(this).attr("disabled")){
					var id=$("#'.$this->table_id.'").jqGrid("getGridParam","selrow");
					if( id != null )
						$("#'.$this->table_id.'").jqGrid("delGridRow",id,{url:"'.$baseUrl.'?operation=delete&id="+id,reloadAfterSubmit:false}); 
					else alert("Выберите строку для удаления");
				}
				return false;
			});';
	
		$control_panel='<div class="control_panel clearfix">';
		if($controls){
			$this->js.=$js;
      	 	$container=new Zend_Navigation($controls);
      	 	$helper=new Zen_View_Helper_Menu_Admin();
      	 	$view=new Zend_View();
      	 	$helper->setView($view);
      	 	$control_panel.=$helper->renderMenu($container,array('ulClass'=>'btn-group ui-corner-all'))."</div>";
		}
			
		return $control_panel;	

	}
	
}
	
